package com.study.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

/*本文件加了User 和 Admin 角色
不同的角色路径需要不同的用户登录才能访问
*/
//@Configuration
//@EnableWebSecurity
public class SecurityConfiguration2 extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/product/**").hasRole("USER")
                .antMatchers("/admin/**").hasRole("ADMIN")
                .anyRequest().authenticated()
            .and()
                .formLogin()
            .and()
                .httpBasic();
    }



    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {



        auth
                .inMemoryAuthentication()
                //.passwordEncoder(new BCryptPasswordEncoder())
                .withUser("admin").password("{noop}adminpass").roles("ADMIN", "USER")
                .and()
                .withUser("spring").password("{bcrypt}$2a$10$dXJ3SW6G7P50lGmMkkmwe.20cQQubK3.HZWzG3YB1tlRy.fqvM/BG").roles("USER");
                //.withUser("spring").password("{noop}password").roles("USER");




    }


    //管理员   用户名admin  密码adminpass  可以访问 /admin/home  也可以访问 /product/info
    //普通    用户名spring   密码password  不可以访问 /admin/home  但可以访问 /product/info

}